package teacher_stu

import (
	"Student_Mental/DBconfing"
	"Student_Mental/model"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"time"
)

func returnErr(err error, c *gin.Context) {
	if err != nil {
		c.JSON(200, gin.H{
			"code": 1,
			"msg":  err.Error(),
		})
	}
}

// SelectClass 根据选择专业查询班级
func SelectClass(c *gin.Context) {
	sqlDb, _ := DBconfing.Mysql()
	rows, err := sqlDb.Query("SELECT * FROM class")
	if err != nil {
		println("结果为空")
		return
	}
	var classList []model.Class
	for rows.Next() {
		var s model.Class
		err = rows.Scan(&s.Id, &s.MajorId, &s.Name)
		data := model.Class{
			Id:      s.Id,
			MajorId: s.MajorId,
			Name:    s.Name,
		}
		classList = append(classList, data)
	}
	c.JSON(200, gin.H{
		"data": classList,
	})
	rows.Close()
	sqlDb.Close()
}

// SelectMajor 专业查询
func SelectMajor(c *gin.Context) {
	sqlDb, _ := DBconfing.Mysql()
	rows, err := sqlDb.Query("SELECT * FROM major")
	if err != nil {
		println("结果为空")
		return
	}
	var majorList []model.Major
	for rows.Next() {
		var s model.Major
		err = rows.Scan(&s.Id, &s.Name)
		data := model.Major{
			Id:   s.Id,
			Name: s.Name,
		}
		majorList = append(majorList, data)
	}
	c.JSON(200, gin.H{
		"data": majorList,
	})
	rows.Close()
	sqlDb.Close()
}

// GetMajor_Class 获取专业名字和班级名字
func GetMajor_Class(Mid int, Cid int) (string, string) {
	sqlDb, _ := DBconfing.Mysql()
	var Major, Class string
	sqlDb.QueryRow("SELECT major_name FROM major where id=?", Mid).Scan(&Major)
	sqlDb.QueryRow("SELECT class_name FROM class where id=?", Cid).Scan(&Class)
	sqlDb.Close()
	return Major, Class
}

// ListData
// @Tags 老师端-学生管理
// @summary 学生列表
// @Router /teacher/stu_list [get]
func ListData(c *gin.Context) {
	var stu_list []model.Student
	sqlDb, _ := DBconfing.Mysql()
	rows, err := sqlDb.Query("SELECT student.*,class.class_name,major.major_name FROM(student inner join class on student.Class_id = class.id) INNER JOIN major ON student.Major_id=major.id")
	if err != nil {
		println("结果为空")
		return
	}
	//遍历打印
	for rows.Next() {
		var s model.Student
		err = rows.Scan(&s.Id, &s.ClassId, &s.MajorId, &s.Number, &s.Password, &s.Name, &s.Sex, &s.Age, &s.CreateTime, &s.Avatar, &s.Class, &s.Major)
		data := model.Student{
			Id:         s.Id,
			ClassId:    s.ClassId,
			Class:      s.Class,
			MajorId:    s.MajorId,
			Major:      s.Major,
			Name:       s.Name,
			Number:     s.Number,
			Password:   s.Password,
			Age:        s.Age,
			Sex:        s.Sex,
			StringTime: s.CreateTime.Format("2006-01-02 15:04:05"),
			Avatar:     s.Avatar,
		}
		stu_list = append(stu_list, data)
	}
	c.JSON(200, gin.H{
		"code": 0,
		"data": stu_list,
	})
	//用完关闭
	rows.Close()
	sqlDb.Close()
}

// Add
// @Tags 老师端-学生管理
// @summary 学生添加
// @Param Number formData  int false "学号"
// @Param Name formData  string false "名字"
// @Param Age formData  int false "年龄"
// @Param Sex formData  string false "性别"
// @Param Class_id formData  int false "班级ID"
// @Param Major_id formData  int false "专业ID"
// @Router /teacher/stu_add [post]
func Add(c *gin.Context) {
	sqlDb, _ := DBconfing.Mysql()
	var stu model.Student
	stu.Number, _ = strconv.Atoi(c.PostForm("Number"))
	stu.Name = c.PostForm("Name")
	stu.Password = "123456"
	stu.Age, _ = strconv.Atoi(c.PostForm("Age"))
	stu.Sex = c.PostForm("Sex")
	stu.ClassId, _ = strconv.Atoi(c.PostForm("Class_id"))
	stu.MajorId, _ = strconv.Atoi(c.PostForm("Major_id"))
	stu.CreateTime = time.Now()
	stu.Avatar = "/static/images/avatar/student/R-C2.png"
	if stu.Name != "" {
		rs, err := sqlDb.Exec("insert into student(Class_id, Major_id, student_number,student_password,student_name,student_sex,student_age,create_time,avatar)values(?,?,?,?,?,?,?,?,?)", stu.ClassId, stu.MajorId, stu.Number, stu.Password, stu.Name, stu.Sex, stu.Age, stu.CreateTime, stu.Avatar)
		returnErr(err, c)
		rowCount, err := rs.RowsAffected()
		returnErr(err, c)
		if rowCount > 0 {
			c.JSON(200, gin.H{
				"code": 0,
				"msg":  "添加成功",
			})
		} else {
			returnErr(err, c)
		}
	}
	sqlDb.Close()
}

// Del
// @Tags 老师端-学生管理
// @summary 学生删除
// @Param id path string false "学生ID"
// @Router /teacher/stu/{id} [delete]
func Del(c *gin.Context) {
	sqlDb, _ := DBconfing.Mysql()
	id := c.Param("id")
	sqlStr := "delete from student where id = ?"
	res, err := sqlDb.Exec(sqlStr, id)
	returnErr(err, c)
	reslist, _ := res.RowsAffected()
	if reslist > 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
		})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"code": 1,
		})
	}
	sqlDb.Close()
}

// GetId
// @Tags 老师端-学生管理
// @summary 获取学生
// @Param id path string false "学生ID"
// @Router /teacher/stu/{id} [get]
func GetId(c *gin.Context) {
	sqlDb, _ := DBconfing.Mysql()
	id := c.Param("id")
	var s model.Student
	err := sqlDb.QueryRow("SELECT * FROM student where id=?", id).Scan(&s.Id, &s.ClassId, &s.MajorId, &s.Number, &s.Password, &s.Name, &s.Sex, &s.Age, &s.CreateTime, &s.Avatar)
	returnErr(err, c)
	c.HTML(http.StatusOK, "student_edit.html", gin.H{
		"Id":       s.Id,
		"Name":     s.Name,
		"Number":   s.Number,
		"Password": s.Password,
		"Age":      s.Age,
		"Sex":      s.Sex,
		"Major":    s.MajorId,
		"Class":    s.ClassId,
		"Avatar":   s.Avatar,
	})
	sqlDb.Close()
}

// SelectStu
// @Tags 老师端-学生管理
// @summary 搜索学生
// @Param msg query string false "搜索信息"
// @Router /teacher/stu_list2 [get]
func SelectStu(c *gin.Context) {

	msg := c.Query("msg")
	sqlDb, _ := DBconfing.Mysql()
	rows, err := sqlDb.Query("SELECT student.*,class.class_name,major.major_name FROM(student inner join class on student.Class_id = class.id) INNER JOIN major ON student.Major_id=major.major_id WHERE Student_number LIKE ? or Student_name LIKE ?", "%"+msg+"%", "%"+msg+"%")
	if err != nil {
		println("结果为空")
		return
	}
	//遍历打印
	var stu_list []model.Student
	for rows.Next() {
		var s model.Student
		err = rows.Scan(&s.Id, &s.ClassId, &s.MajorId, &s.Number, &s.Password, &s.Name, &s.Sex, &s.Age, &s.CreateTime, &s.Avatar, &s.Class, &s.Major)
		data := model.Student{
			Id:         s.Id,
			ClassId:    s.ClassId,
			Class:      s.Class,
			MajorId:    s.MajorId,
			Major:      s.Major,
			Name:       s.Name,
			Number:     s.Number,
			Password:   s.Password,
			Age:        s.Age,
			Sex:        s.Sex,
			StringTime: s.CreateTime.Format("2006-01-02 15:04:05"),
			Avatar:     s.Avatar,
		}
		stu_list = append(stu_list, data)
	}

	c.JSON(200, gin.H{
		"code": 0,
		"data": stu_list,
		"msg":  msg,
	})
	//用完关闭
	rows.Close()
	sqlDb.Close()
}

// Edit
// @Tags 老师端-学生管理
// @summary 提交修改
// @Param id formData  int false "学生ID"
// @Param Number formData  int false "学号"
// @Param Password formData  string false "密码"
// @Param Name formData  string false "名字"
// @Param Age formData  int false "年龄"
// @Param Sex formData  string false "性别"
// @Param Class_id formData  int false "班级ID"
// @Param Major_id formData  int false "专业ID"
// @Router /teacher/stu/{id} [put]
func Edit(c *gin.Context) {
	sqlDb, _ := DBconfing.Mysql()
	var stu model.Student
	stu.Id, _ = strconv.Atoi(c.Param("id"))
	stu.Number, _ = strconv.Atoi(c.PostForm("Number"))
	stu.Password = c.PostForm("Password")
	stu.Name = c.PostForm("Name")
	stu.Age, _ = strconv.Atoi(c.PostForm("Age"))
	stu.Sex = c.PostForm("Sex")
	stu.ClassId, _ = strconv.Atoi(c.PostForm("Class_id"))
	stu.MajorId, _ = strconv.Atoi(c.PostForm("Major_id"))
	stu.CreateTime = time.Now()

	if stu.Name != "" {
		rs, err := sqlDb.Exec("update student set Class_id=?, Major_id=?, student_number=?,student_password=?,student_name=?,student_sex=?,student_age=? where id=?", stu.ClassId, stu.MajorId, stu.Number, stu.Password, stu.Name, stu.Sex, stu.Age, stu.Id)
		returnErr(err, c)
		row, err := rs.RowsAffected()
		returnErr(err, c)
		if row > 0 {
			c.JSON(200, gin.H{
				"code": 0,
				"msg":  "修改成功",
			})
		} else {
			c.JSON(200, gin.H{
				"code": 0,
				"msg":  "无修改",
			})
		}
	}
	sqlDb.Close()
}

// RecordList
// @Tags 老师端-学生管理
// @summary 考试记录列表
// @Param id path string false "学生ID"
// @Router /teacher/stu_test/record/{Id} [get]
func RecordList(c *gin.Context) {
	Id := c.Param("Id")
	sqlDb, _ := DBconfing.Mysql()
	rows, err1 := sqlDb.Query("SELECT * FROM testrecord where stu_id=" + Id)
	returnErr(err1, c)
	var RecordList []model.TestRecord
	for rows.Next() {
		var s model.TestRecord
		rows.Scan(&s.Id, &s.StuId, &s.PaperName, &s.PaperId, &s.ResList, &s.Score, &s.CreateTime)
		data := model.TestRecord{
			Id:         s.Id,
			StuId:      s.StuId,
			PaperName:  s.PaperName,
			PaperId:    s.PaperId,
			ResList:    s.ResList,
			Score:      s.Score,
			StringTime: s.CreateTime.Format("2006-01-02 15:04:05"),
		}
		RecordList = append(RecordList, data)
	}
	c.HTML(200, "stu_recordList.html", gin.H{
		"data": RecordList,
	})
	rows.Close()
	sqlDb.Close()
}

// CheckTheExam
// @Tags 老师端-学生管理
// @summary 答题情况
// @Param id path string false "考试记录id"
// @Param paperId path string false "试卷id"
// @Router /teacher/stu_test/check/{paperId}/{id} [get]
func CheckTheExam(c *gin.Context) {
	recordId := c.Param("id")
	paperId := c.Param("paperId")
	sqlDb, _ := DBconfing.Mysql()
	var resList string
	err := sqlDb.QueryRow("SELECT res_list FROM testrecord where id=" + recordId).Scan(&resList)
	returnErr(err, c)
	c.HTML(200, "stu_checkTest.html", gin.H{
		"paperId": paperId,
		"resList": resList,
	})
	sqlDb.Close()
}
